package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.dto.RecordsDto;
import com.itheima.service.SetmealService;
import com.itheima.vo.RecordsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public CommonResult page(String name, @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                            Integer type) {
        PageInfo<RecordsVo> pageInfo = setmealService.page(name, page, pageSize,type);
        return CommonResult.success(pageInfo);
    }

    @PostMapping
    public CommonResult save(@RequestBody RecordsDto recordsDto) {
        boolean save = setmealService.save(recordsDto);
        if (!save) {
            return CommonResult.error("新增套餐失败");
        }
        return CommonResult.success("新增套餐成功");
    }

    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable String id) {
        RecordsVo recordsVo = setmealService.getById(id);
        if (recordsVo == null) {
            return CommonResult.error("套餐不存在");
        }
        return CommonResult.success(recordsVo);
    }

    @DeleteMapping
    public CommonResult delete(@RequestParam List<String> ids) {
        boolean delete = setmealService.delete(ids);
        if (!delete) {
            return CommonResult.error("套餐正在售卖中，不能删除");
        }
        return CommonResult.success("删除套餐成功");
    }
    @PostMapping("/status/{status}")
    public CommonResult  sell(@RequestParam("ids") List<String> ids,@PathVariable int status){
        boolean sell = setmealService.sell(ids,status);
        if (!sell){
            return CommonResult.error("操作失败");
        }
        return  CommonResult.success("操作成功");
    }
    @PutMapping
    public CommonResult update(@RequestBody RecordsDto recordsDto){
        boolean update = setmealService.update(recordsDto);
        if (!update){
            return CommonResult.error("修改失败");
        }
return  CommonResult.success("修改成功");
    }
}
